package nu.aing.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

/**
 * @author memekmaut@gmail.com
 */
public class TestUtils {
    
    /**
     * @param obj
     * @param methodName
     * @param params
     */
    public static void test(
            Object obj,
            String methodName,
            Object... params) {
        
        StringBuilder sb = new StringBuilder();
        for(Object param : params) {
            if(printArray(param) ) {
                continue;
            }
            sb.append(param).append(", ");
        }
        System.out.println(sb.toString() );
        
        final Method[] methods = obj.getClass().getDeclaredMethods();
        long start = 0l, end = 0l;
        Object result = null;
        for(Method method : methods) {
            try {
                if(method.getName().equals(methodName) ) {
                    int modifiers = method.getModifiers();
                    if (!Modifier.isPublic(modifiers) ) {
                        throw new IllegalArgumentException("Method ["+methodName+"] not accesible.");
                    }
                    start = System.currentTimeMillis();
                    result = method.invoke(obj, params);
                    end = System.currentTimeMillis();
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage() );
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage() );
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage() );
            }
        }
        
        if(result == null) {
            throw new IllegalArgumentException("Result null.");
        }
        
        print(result);
        System.out.println("Time: " + (end - start) / 1000.0 + " seconds");
        System.out.println("========================");
    }
    
    private static void print(Object obj) {
        if(printString(obj) ) {
            return;
        }
        if(printInt(obj) ) {
            return;
        }
        if(printArray(obj) ) {
            return;
        }
    }
    
    private static boolean printInt(Object obj) {
        if(obj instanceof Integer) {
            Integer temp = (Integer) obj;
            System.out.println(temp);
            return true;
        }
        return false;
    }
    
    private static boolean printString(Object obj) {
        if(obj instanceof String) {
            String temp = (String) obj;
            System.out.println(temp);
            return true;
        }
        return false;
    }
    
    private static boolean printArray(Object obj) {
        if(obj instanceof int[]) {
            int[] temp = (int[]) obj;
            System.out.println(Arrays.toString(temp) );
            return true;
        }
        if(obj instanceof String[]) {
            String[] temp = (String[]) obj;
            System.out.println(Arrays.toString(temp) );
            return true;
        }
        
        return false;
    }
}
